/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PhuocPH;

import Entity.*;
import Entity.FeedBack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**

 */
public class PhuocPHServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            HttpSession session = request.getSession(true);
            Context ctx = new InitialContext();
            PhuocPHSessionBeanLocal PLocal = (PhuocPHSessionBeanLocal) ctx.lookup("PhuocPHSessionBeanLocalJNDI");
            if(action.equals("GetListCate"))
            {
                Category[] resultCate = PLocal.GetCategory();
                request.getSession().setAttribute("List_Category", resultCate);
                request.getRequestDispatcher("ManageCategory.jsp").forward(request, response);
            }else if (action.equals("Add New"))
            {
                 if(PLocal.InsertCategory(request.getParameter("txtCateName")))
                 {
                      request.getRequestDispatcher("PhuocPHServlet?action=GetListCate").forward(request, response);
                 }
            }else if(action.equals("Delete"))
            {
                if(PLocal.DeleteCategory(Integer.parseInt(request.getParameter("Cate_ID"))))
                {
                     request.getRequestDispatcher("PhuocPHServlet?action=GetListCate").forward(request, response);
                }
            }else if(action.equals("Edit"))
            {
                 Category objCate = PLocal.findCategoryByPK(Integer.parseInt(request.getParameter("Cate_ID")));
                 request.getSession().setAttribute("Cate_obj", objCate);
                 request.getRequestDispatcher("ADViewDetailCategory.jsp").forward(request, response);
            }else if(action.equals("Save"))
            {
                  if(PLocal.UpdateCategory(Integer.parseInt(request.getParameter("txtCate_ID")), request.getParameter("txtCate_Name")))
                  {
                       request.getRequestDispatcher("PhuocPHServlet?action=GetListCate").forward(request, response);
                  }
            }
            else if(action.equals("ManageFB"))
            {
                
                FeedBack[] listFB = PLocal.getFeedBack();
                session.setAttribute("List_FB", listFB);
                request.getRequestDispatcher("ADManagerFB.jsp").forward(request, response);
            }
            else if(action.equals("detailFB"))
            {
                int id = Integer.parseInt(request.getParameter("txtid"));
                FeedBack objFB = PLocal.findFeedBackByPK(id);
                session.setAttribute("DETAILFB", objFB);
                request.getRequestDispatcher("ADDetailFeedBack.jsp").forward(request, response);
            }
            else if(action.equals("Reply"))
            {
                int id = Integer.parseInt(request.getParameter("txtid"));
                String rep = request.getParameter("txtReply1");
                if(PLocal.ReplyFB(id, rep))
                {
                  
                   request.getRequestDispatcher("PhuocPHServlet?action=ManageFB").forward(request, response);
                }
            }
            else if(action.equals("goRepFB"))
            {
                 FeedBack objFB = PLocal.findFeedBackByPK(Integer.parseInt(request.getParameter("txtidrep")));
                 request.getSession().setAttribute("FBobj", objFB);
                 request.getRequestDispatcher("ADReplyFeedBack.jsp").forward(request, response);
            }
            else if(action.equals("UViewFB"))
            {
                Account objA = (Account)session.getAttribute("User_Session");
                String us = objA.getUsername();
                AccountFeedBackDTO listFBbyAccount = PLocal.findFeedBackByAccount(us);
                session.setAttribute("listFbAccount", listFBbyAccount);
                request.getRequestDispatcher("UserViewFB.jsp").forward(request, response);
            }
            else if(action.equals("Send"))
            {
                Account objA = (Account) session.getAttribute("User_Session");
                String us = objA.getUsername();
                String titte = request.getParameter("txtTitle");
                String content = request.getParameter("txtContent");
                if(PLocal.SendFB(us, titte, content))
                {
                    request.getRequestDispatcher("PhuocPHServlet?action=UViewFB").forward(request, response);
                }
            }
            else if(action.equals("UserDetailFB"))
            {
                int id = Integer.parseInt(request.getParameter("txtidFB"));
                FeedBack objF = PLocal.findFeedBackByPK(id);
                session.setAttribute("USDETAILFB", objF);
                request.getRequestDispatcher("USDetailFB.jsp").forward(request, response);
            }
            else if(action.equals("Comment"))
            {
                Account objA = (Account) session.getAttribute("User_Session");
                String us = objA.getUsername();
                String com = request.getParameter("txtComment");
                int idEV = Integer.parseInt(request.getParameter("txtEvid"));
                if(PLocal.sendComment(idEV, us, com))
                {
                    request.getRequestDispatcher("VietDQServlet?action=USDetailEV&txtid="+idEV).forward(request, response);
                }
            }
            else if(action.equals("manageComment"))
            {
                Comment[] listCom = PLocal.getComment();
                session.setAttribute("ADVIEWCOM", listCom);
                request.getRequestDispatcher("ManageComment.jsp").forward(request, response);
            }
            else if(action.equals("DetailCom"))
            {
                int id = Integer.parseInt(request.getParameter("txtIdCom"));
                Comment objCo = PLocal.findCommentByPK(id);
                session.setAttribute("DETAILCOM", objCo);
                request.getRequestDispatcher("DetailComment.jsp").forward(request, response);
            }
            else if(action.equals("Close"))
            {
                if(PLocal.disableComment(Integer.parseInt(request.getParameter("idClose"))))
                {
                    request.getRequestDispatcher("PhuocPHServlet?action=manageComment").forward(request, response);
                }
            }
            else if(action.equals("Open"))
            {
                if(PLocal.enableComment(Integer.parseInt(request.getParameter("idOpen"))))
                {
                    request.getRequestDispatcher("PhuocPHServlet?action=manageComment").forward(request, response);
                }
            }
            else if(action.equals("USVIEWCOM"))
            {
                Account objA = (Account)session.getAttribute("User_Session");
                String us = objA.getUsername();
                AccountCommentDTO listDTO = PLocal.findCommentByAccount(us);
                session.setAttribute("listComAccount", listDTO);
                request.getRequestDispatcher("UsManageComment.jsp").forward(request, response);
            }
          }catch(Exception e){
            e.printStackTrace();
          } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
